// pages/tingnai/tingnai.js
var biangengData = require("../biangengdata/biangengdata.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: '',//订单
    itemsNo: false,
    qdrq: '2020-09-01',
    tdrq: '2020-09-01',
    pushWrapBtn: true,
    letterWrapBtn: true,
    letterBtnSuccess: false,
    changeBtnBg: true,
    numberBiangeng: '1',//变更数量
    productNameSum: '',//停奶产品名称
    noProductSum: false,//无选中产品
    submitOne: false,
    dayNum:1,//变更天数
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      items: biangengData.items,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  checkboxChange(e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    var itemsNo = false;
    if (e.detail.value.length > 0) {
      itemsNo = true
    } else {
      console.log('00')
      itemsNo = false
    }

    const items = this.data.items
    const values = e.detail.value;
    var select = ''
    for (let i = 0, lenI = items.length; i < lenI; ++i) {
      items[i].checked = false
      for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
        if (items[i].value === values[j]) {
          items[i].checked = true
          break
        }
      }
      for (let s = 0, lenI = items[i].order.length; s < lenI; ++s) {
        if (items[i].checked == false) {
          items[i].order[s].checkedProduct = false;
        }
        if (items[i].order[s].checkedProduct == true) {
          select += items[i].order[s].text;
        }
      }
    }
    var noProductSum = this.data.noProductSum;
    if (select == '') {
      noProductSum = false;
    } else {
      noProductSum = true;
    }
    this.setData({
      items,
      itemsNo: itemsNo,
      productNameSum: select,
      noProductSum: noProductSum
    })
  },
  checkproductChange(e) {//选择产品
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    const items = this.data.items
    const values = e.detail.value;
    for (let n = 0, lenI = items.length; n < lenI; ++n) {
      for (let i = 0, lenI = items[n].order.length; i < lenI; ++i) {
        items[n].order[i].checkedProduct = false
        for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
          if (items[n].order[i].index === values[j]) {
            items[n].order[i].checkedProduct = true;
            break
          }
        }
      }
    }
    var productNameSum = ''
    for (var m = 0; m < values.length; ++m) {
      var countIndex = values[m].split("-");
      productNameSum += items[countIndex[0]].order[countIndex[1]].text
    }
    var noProductSum = this.data.noProductSum;
    if (productNameSum == '') {
      noProductSum = false;
    } else {
      noProductSum = true;
    }
    this.setData({
      items,
      productNameSum: productNameSum,
      noProductSum: noProductSum
    })
    console.log(this.data.noProductSum)
  },
  add: function (e) {
    // 加
    var count = this.data.numberBiangeng;
    count++
    this.setData({
      numberBiangeng: count,
    });
  },
  minus: function (e) {
    // 减
    var count = this.data.numberBiangeng;
    if (count <= 1) {
      count = 1
    } else {
      count--;
    }
    this.setData({
      numberBiangeng: count,
    })
  },
  countSum: function () {
    // 总价
    var count = this.data.specification;
    var milkingSelectDay = this.data.milkingSelectDay;
    count.priceSumday = (count.priceSum * milkingSelectDay).toFixed(2);
    this.setData({
      specification: count,
      'dn.je': count.priceSumday
    })
  },
  gotoKsrq: function (e) { //获取开始日期
    this.setData({
      qdrq: e.detail.value
    })
    this.checkDate(this.data.qdrq, this.data.tdrq)
  },
  gotoJzrq: function (e) { //获取结束日期
    this.setData({
      tdrq: e.detail.value,
    })
    this.checkDate(this.data.qdrq, this.data.tdrq)
  },
  checkDate: function (startTime, endTime) {
    //日期格式化
    var start_date = new Date(startTime.replace(/-/g, "/"));
    var end_date = new Date(endTime.replace(/-/g, "/"));
    //转成毫秒数，两个日期相减
    var ms = end_date.getTime() - start_date.getTime();
    //转换成天数
    var day = parseInt(ms / (1000 * 60 * 60 * 24));
    if (day < 0) {
      day = 0
      this.setData({
        tdrq: startTime,
      })
    }
    this.setData({
      dayNum: day + 1
    })
  },
  fondPush: function () {
    var pushWrapBtn = this.data.pushWrapBtn;
    console.log(pushWrapBtn)
    this.setData({
      pushWrapBtn: !pushWrapBtn
    })
  },
  pushFs: function () {//微信示例
    var letterWrapBtn = this.data.letterWrapBtn;
    this.setData({
      letterWrapBtn: !letterWrapBtn
    })
  },
  letterBtnSuccess: function () {
    var that = this
    that.setData({
      letterBtnSuccess: true
    })
    wx.showToast({
      title: '发送成功',
      icon: 'success',
      duration: 500
    })
    setTimeout(function () {
      that.setData({
        letterBtnSuccess: false,
        letterWrapBtn: true
      });
    }, 500)
  },
  changeBtnBg: function () {
    var changeBtnBg = this.data.changeBtnBg;
    this.setData({
      changeBtnBg: !changeBtnBg
    })
  },
  submit: function () {
    this.setData({
      submitOne: true
    });
  },
})